<template>
  <div>
    用户头像修改
    <el-upload
      class="avatar-uploader"
      action="https://jsonplaceholder.typicode.com/posts/"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
      :auto-upload="false"
      ref="upload"
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar" />
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>

    <el-button type="primary" @click="updateImg">上传图片</el-button>
  </div>
</template>

<script>
import { abc } from "../api/requestapi";
export default {
  name: "UserImg",

  data() {
    return {
      imageUrl: "",
      file: "",
    };
  },

  methods: {
    updateImg() {
      var formdata = new formdata();
      formdata.append("file", this.file);

      updateUserImg(formdata).then((response) => {
        console.log("上传成功了");
      });
    },

    //上传前检验文件类型
    beforeAvatarUpload(file) {
      this.file = file;

      this.imageUrl = URL.createObjectURL(file.raw);
    },

    //成功之后 更新vuex图片
    handleAvatarSuccess() {},
  },
};
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>